import sys,os,time,datetime
def SelectDir():
    if os.path.exists('d:\\'):
        Logdir = 'd:\\log'
        return Logdir
    elif os.path.exists('c:\\'):
        Logdir  =   'c:\\log'
        return Logdir
    else:
        return print("没有C盘和D盘你让我怎么办？？？？？")
def DeleteLog(fullname):
    try:
       os.remove(fullname)
       deleteLog(fullname)
       #print(fullname+"文件已经被删除")
       return True
    except Exception:
        print('文件不存在')
def SeachFile(path):
    for dirpath, dirnames, filenames in os.walk(path):
        for file in filenames:
            fullpath = os.path.join(dirpath, file)
            SelectTimeFile(fullpath)
def SelectTimeFile(filepath):
    mtime=time.ctime(os.path.getmtime(filepath))
    f_time = datetime.datetime.strptime(mtime,'%a %b %d %H:%M:%S %Y')
    #print(filepath)
    NowTime=time.strftime('%a %b %d %H:%M:%S %Y')
    now_Time=datetime.datetime.strptime(NowTime,'%a %b %d %H:%M:%S %Y')
    #print(f_time)
    if (now_Time - f_time).days >= 7:
        DeleteLog(filepath)
        return print(filepath+"\n>>>>> 时间大于7天----已经删除----")
    else:
        return print('>>>>>当前日志时间未到7天')
def deleteLog(filename):
    NowTime = time.strftime('%a %b %d %H:%M:%S %Y')
    #now_Time = datetime.datetime.strptime(NowTime, '%a %b %d %H:%M:%S %Y')
    fb = open('d:\\test.txt', 'a')
    fb.write(filename+"\t<<<<<<<<<<<已经删除>>>>>>>>>>"+NowTime+'\n')
    fb.close()


if __name__ == '__main__':
    while True:
        Dir = SelectDir()
        print('当前目录是：' + Dir)
        SeachFile(Dir)
        time.sleep(43200)
